package com.sunxueqi.project.system.service.impl;

import java.sql.Timestamp;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sunxueqi.common.constant.StatusConstants;
import com.sunxueqi.common.utils.KeyGenerate;
import com.sunxueqi.common.utils.SecurityUtils;
import com.sunxueqi.project.system.dao.CarouselMapper;
import com.sunxueqi.project.system.service.ICarouselService;
import com.sunxueqi.project.system.vo.Carousel;

@Service
public class CarouselServiceImpl implements ICarouselService{
	
	@Autowired
	private CarouselMapper carouselMapper;
	
	@Override
	public List<Carousel> selectCarouselList(Carousel carousel) {
		carousel.setDeleteStatus(StatusConstants.NORMAL);
		return carouselMapper.selectList(carousel);
	}

	@Override
	public int add(Carousel carousel) {
		carousel.setId(KeyGenerate.getKeyId());
		carousel.setDeleteStatus(StatusConstants.NORMAL);
		carousel.setCreateBy(SecurityUtils.getLoginUserId());
		carousel.setCreateTime(new Timestamp(System.currentTimeMillis()));
		return carouselMapper.insert(carousel);
	}

	@Override
	public int deleteCarouselByIds(String[] idArray) {
		int result = 0;
		for(String id: idArray) {
			Carousel Carousel = new Carousel();
			Carousel.setId(id);
			Carousel.setDeleteStatus(StatusConstants.DELETED);
			Carousel.setDeleteTime(new Timestamp(System.currentTimeMillis()));
			Carousel.setDeleteBy(SecurityUtils.getLoginUserId());
			int count = carouselMapper.updateByPrimaryKey(Carousel);
			result += count;
		}
		return result;
	}

	@Override
	public int updateCarousel(Carousel carousel) {
		carousel.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		carousel.setUpdateBy(SecurityUtils.getLoginUserId());
		int count = carouselMapper.updateByPrimaryKey(carousel);
		return count;
	}

	@Override
	public int changeStatus(Carousel carousel) {
		carousel.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		carousel.setUpdateBy(SecurityUtils.getLoginUserId());
		return carouselMapper.updateByPrimaryKey(carousel);
	}

	@Override
	public int changeTarget(Carousel carousel) {
		carousel.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		carousel.setUpdateBy(SecurityUtils.getLoginUserId());
		return carouselMapper.updateByPrimaryKey(carousel);
	}
}
